package hw.ats.news.ds;

import hw.ats.news.model.Cate;

import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.util.ByteArrayBuffer;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class CateParser {
	public List<Cate> parse(String urlstring) {
		List<Cate> results = null;
        try {
            URL url = new URL(urlstring);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            URLConnection con = url.openConnection();
            InputStream is = con.getInputStream();
            ByteArrayBuffer bab = new ByteArrayBuffer(1000);
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = is.read(b)) != -1) {
            	bab.append(b, 0, len);
            }
            
            String xml = new String(bab.toByteArray(), "UTF-8");
            XmlHandler handler = new XmlHandler();
            xr.setContentHandler(handler);
            xr.parse(new InputSource(new StringReader(xml)));
            results = handler.getItems();
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (results == null) {
        	results = new ArrayList<Cate>();
        }
        
        return results;
	}
	
	class XmlHandler extends DefaultHandler {
	    private static final String ITEM = "category";
	    private static final String UID = "id";
	    private static final String TITLE = "name";
	    private static final String ICON_URL = "icon_url";
	    
	    private boolean startEntry = false;
	    private Cate item = null;
	    private StringBuilder buf = null;
		private final ArrayList<Cate> items;
		
	    public XmlHandler() {
	        items = new ArrayList<Cate>();
	        buf = new StringBuilder();
	    }
	    
	    @Override
	    public void startDocument() throws SAXException {
	    }
	  
	    @Override
	    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 
	    throws SAXException {
	        if (localName.equals(XmlHandler.ITEM)) {
	            startEntry = true;
	            item = new Cate();
	            buf.setLength(0);
	        }
	    }
	    
	    @Override
	    public void endDocument() throws SAXException {
	    }

	    @Override
	    public void endElement(String namespaceURI, String localName, String qName) 
	    throws SAXException {
	        if (localName.equals(XmlHandler.ITEM)) {
	            startEntry = false;
	            items.add(item);
	            item = new Cate();
	            item.pid = 0;
	        }

	        if (startEntry) {
	        	if (localName.equals(XmlHandler.UID)) {
	            	item.uid = Integer.parseInt(buf.toString().trim());
	            } else if (localName.equals(XmlHandler.TITLE)) {
	            	item.title = buf.toString().trim();
	            } else if (localName.equals(XmlHandler.ICON_URL)) {
	            	item.icon = buf.toString().trim();
	            }
	            
	            buf.setLength(0);
	        }
	    }   
	    
	    @Override
	    public void characters(char[] ch, int start, int length) {
	        if (ch != null) {
	            buf.append(new String(ch, start, length));
	        }
	    }
	    
	    public ArrayList<Cate> getItems() {
	        return items;
	    }
	}
}
